package com.dreamwhale.machinery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.config.DataResult;
import com.dreamwhale.machinery.dao.DictDetailMapper;
import com.dreamwhale.machinery.entity.DictDetail;
import com.dreamwhale.machinery.entity.UserApply;
import com.dreamwhale.machinery.dao.UserApplyMapper;
import com.dreamwhale.machinery.goods.entity.YxStoreOrder;
import com.dreamwhale.machinery.pay.ali.server.AliPayService;
import com.dreamwhale.machinery.pay.weChat.server.WeChatPayServer;
import com.dreamwhale.machinery.service.UserApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dreamwhale.machinery.utils.WorkIdUtils;
import com.sun.corba.se.spi.ior.ObjectKey;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Author
 * @since 2020-06-13
 */
@Service
public class UserApplyServiceImpl extends ServiceImpl<UserApplyMapper, UserApply> implements UserApplyService {

    @Resource
    private AliPayService aliPayService;

    @Resource
    private WeChatPayServer weChatPayServer;

    @Resource
    private DictDetailMapper dictDetailMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public DataResult userApply(String name, String phone, Integer payType, Integer userId, Integer pid) {
        DictDetail dictDetail = dictDetailMapper.selectOne(new QueryWrapper<DictDetail>().eq("label", "apply"));
        UserApply userApply = new UserApply();
        String orderId = WorkIdUtils.generateOrderId();
        userApply.setApplyCreateTime(LocalDateTime.now());
        userApply.setApplyOrderId(orderId);
        userApply.setApplyName(name);
        userApply.setApplyPhone(phone);
        userApply.setApplyMoney(BigDecimal.valueOf(Double.parseDouble(dictDetail.getValue())));
        if (this.save(userApply)) {
            //  resultMap
            switch (payType) {
                case 1:
                    return aliPayService.orderApply(orderId, userId, pid);
                case 2:
                    return weChatPayServer.orderApply(orderId, userId, pid, null, null);
                default:
                    throw new RuntimeException("未知支付方式");
            }
        }
        return DataResult.ok();
    }

    @Override
    public DataResult inquiryApplyInfo() {
        DictDetail apply = dictDetailMapper.selectOne(new QueryWrapper<DictDetail>().eq("label", "apply"));
        DictDetail applyInfo = dictDetailMapper.selectOne(new QueryWrapper<DictDetail>().eq("label", "applyInfo"));
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("pic", apply == null ? BigDecimal.ZERO : apply.getValue());
        resultMap.put("info", applyInfo == null ? "" : applyInfo.getValue());
        return new DataResult(resultMap);
    }
}
